<?php

class internal_do extends CActiveRecord {
/**
 * The followings are the available columns in table 'cargo':
 * @var double $id
 * @var string $ref_no
 * @var double $in_date
 * @var double $out_date
 * @var string $qty
 * @var double $wt
 * @var double $m3
 * @var integer $con_id
 * @var integer $cus_id
 */

/**
 * Returns the static model of the specified AR class.
 * @return CActiveRecord the static model class
 */

    public $Transaction,$invoice,$details,$courier,$description;


    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'cargo';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        return array(
        array('desc','safe'),
        array('con_id, cus_id', 'required'),
        array('con_id, cus_id', 'numerical', 'integerOnly'=>true),
        );
    }
    /**
     * @return array relational rules.
     */
    public function relations() {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
        return array(
        'subcert'=>array(self::BELONGS_TO,'subcert','subcert_id'),
        'consignee'=>array(self::BELONGS_TO,'company','con_id'),
        'customer'=>array(self::BELONGS_TO,'company','cus_id'),
        'details'=>array(self::HAS_MANY,'cargo_details','do_id'),
        'packing'=>array(self::BELONGS_TO,'packing_list_details','packing_detail_id'),
        );
    }
    public function getDO($datas) {
        foreach($datas as $data)
            $do .= $data['ref_no'].',';
        return $do;
    }

    public function testing($datas) {
        foreach($datas as $data) {
          $returnVal .= $data->ref_no;
      }
      return $returnVal;
    }
    public function validateDO()
    {
        //validate every model
        $validate = true;
        foreach($this->courier as $ref) {
            if(!$ref->validate())
                $validate = false;
        }
        if(is_array($this->invoice)) {
            foreach($this->invoice as $ref) {
                if(!$ref->validate())
                    $validate = false;
            }
        }
        elseif(!$this->invoice->validate())
            $validate = false;
        foreach ($this->description as $ref) {
            if(!$ref->validate()) {
                Yii::log("Internal DO description validating error", 'error', 'application.cargo');
                $validate = false;
            }
        }
        foreach($this->details as $ref) {
            if(!$ref->validate())
                $validate = false;
        }
        if(!$this->validate())
            $validate = false;

        return $validate;

        // End validate
    }
    /**
     *
     *
     * @return array customized attribute labels (name=>label)
     */

    public function saveDo($validate = true) {

        if($validate)
            if(!$this->validateDO())
                return false;
        //Save internal DO
        if(!$this->save(false)) {
            return false;
        }
        //End save internal DO
        // Save invoice
	
        //deleting all related references
        invoice::model()->deleteAll("do_id=:do_id",array(':do_id'=>$this->id));
        //saving invoice
        if(is_array($this->invoice)) {
            foreach($this->invoice as $invoice) {
                $invoice->do_id = $this->id;
                if(!$invoice->save(false))
                    return false;
            }
        }
        //End save invoice
        //Save Courier
        foreach($this->courier as $courier) {
            $courier->do_id = $this->id;
            if(!$courier->save(false))
                return false;
        }
        //End save Description
        // Save new details
        foreach($this->details as $detail) {
            $detail->do_id = $this->id;
            if(!$detail->save(false)) {
                return false;
            }
            else {
            //Save Description
                cargo_packing_list::model()->deleteAll('do_details_id=:detail_id', array(':detail_id'=>$detail->id));
                foreach($this->description as $description) {
                    echo ($description->do_details_id.':'.$detail->ref_no);
                    if(strtoupper(trim($description->do_details_id)) == strtoupper(trim($detail->ref_no))) {
                        $old_details_id = $description->do_details_id;
                        $description->do_details_id = $detail->id;
                        if(!$description->save(false)) {
                            Yii::log("Internal DO descriptions saving error",'error','application.cargo');
                            $description->do_details_id = $old_details_id;
                            return false;
                        }
                        $description->do_details_id = $old_details_id;
                        //unset($this->description[$i]);
                    }
                }
            //End save Description
            }
        }
        // End save details

        // Save subcert



        //End save subcert

        // Save import

        //End save import
        return true;
    }

    public function attributeLabels() {
        return array(
        'id'=>'Id',
        'inv_no'=>'Invoice No.',
        'ref_no'=>'Shipper No.',
        'in_date'=>'Arrival Date',
        'out_date'=>'Departure Date',
        'desc'=>'Remark',
        'qty'=>'Quality',
        'wt'=>'Weight',
        'm3'=>'M3',
        'amt'=>'Amount',
        'con_id'=>'Consignee',
        'cus_id'=>'Customer',
        );
    }

}